class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) 
    {
        if(!strs.size())
        {
            return "";
        }
        int num = strs.size();
        std::string res = strs[0];
        for(int i = 1; i < num; i++)
        {
            res = calTwoCommon(res, strs[i]);
            if(!res.size())
            {
                return "";
            }
        }
        return res;

    }

    std::string calTwoCommon(const std::string str1, const std::string str2)
    {
        int length = std::min(str1.size(), str2.size());
        int idx = 0;
        while(idx < length && str1[idx] == str2[idx])
        {
            idx += 1;
        }
        std::string res = str1.substr(0, idx);
        return res;
    }
};